package com.BlogPark.controller;

import com.BlogPark.controller.exception.Code;
import com.BlogPark.controller.result.Result;
import com.BlogPark.pojo.Article;
import com.BlogPark.service.ArticleService.ArticleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: MFM
 * @Date: 2022/8/10 - 08 - 10 - 22:10
 * @version: 1.0
 * 我的文章 管理  增删改查
 */

@RestController
@RequestMapping("/MyArticle")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @DeleteMapping("/{id}")
    public Result deleteArticle(@PathVariable String id){

        Integer data = articleService.deleteArticleById(id);
        if (data>0){

            return new Result(Code.DELETE_OK,data,"删除成功");
        }
        return new Result(Code.DELETE_ERR,null,"删除失败");

    }

    @PostMapping
    public Result saveArticle(@RequestBody Article article){
        boolean flag = articleService.publishArticle(article);
        String data = article.getId();
        if (flag){
            return new Result(Code.SAVE_OK,flag,"发布成功");
        }
        return new Result(Code.SAVE_ERR,null,"发布失败");

    }

    @PutMapping
    public Result updateArticle(@RequestBody Article article){
        Integer data = articleService.updateArticle(article);
        if (data>0){
            return new Result(Code.UPDATE_OK,data,"修改成功");
        }
        return new Result(Code.UPDATE_ERR,null,"修改失败");

    }

    @GetMapping("/{userId}")
    public Result selectAllMyArticle(@PathVariable int userId){
        List<Article> data = articleService.selectAllArticle(userId);
        if (data.size()>0){
            return new Result(Code.GET_OK,data,"查询成功");
        }
        return new Result(Code.GET_ERR,null,"查询失败");
    }
    @GetMapping()
    public Result selectAllArticle(){
        List<Article> data = articleService.selectAllArticle();
        if (data.size()>0){
            return new Result(Code.GET_OK,data,"查询成功");
        }
        return new Result(Code.GET_ERR,null,"查询失败");
    }




}
